package com.bangyu.monitor.modular.system.service;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.bangyu.monitor.core.common.exception.BizExceptionEnum;
import com.bangyu.monitor.core.common.page.LayuiPageFactory;
import com.bangyu.monitor.modular.system.entity.Bridge;
import com.bangyu.monitor.modular.system.mapper.BridgeMapper;
import com.bangyu.monitor.modular.system.model.BridgeDto;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: zhangxue
 * @Date: 19/4/1 13:47
 * @Description:
 */
@Service public class BridgeService extends ServiceImpl<BridgeMapper, Bridge> {

    /**
     * 所有桥梁
     * @param briegeName
     * @return
     */
    public Page<Bridge> pageAll(String briegeName) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.pageAll(page, briegeName);
    }

    /**
     * 分页查询负责人下的桥梁
     * @param managerPersonId
     * @param bridgeName
     * @return
     */
    public Page<Bridge> pageByManagerPerson(Long managerPersonId,String bridgeName){
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.pageByManagerPerson(page,managerPersonId,bridgeName);
    }

    /**
     * 分页查询游客可见的桥梁
     * @param bridgeName
     * @return
     */
    public Page<Bridge> pageByShowToTourist(String bridgeName){
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.pageByShowToTourist(page,bridgeName);
    }


    /**
     * 所有桥梁
     * @param condition
     * @return
     */
    public List<Bridge> listAll(String condition) {
        return this.baseMapper.listAll(condition);
    }

    /**
     * 查询负责人下的桥梁列表
     * @param managerPersonId
     * @param condition
     * @return
     */
    public List<Bridge> listByManagerPerson(Long managerPersonId,String condition){
        return this.baseMapper.listByManagerPerson(managerPersonId,condition);
    }

    /**
     * 查询游客可见的桥梁列表
     * @param condition
     * @return
     */
    public List<Bridge> listByShowToTourist(String condition){
        return this.baseMapper.listByShowToTourist(condition);
    }

    public void addBridge(BridgeDto bridgeDto) {

        String deviceNo = bridgeDto.getDeviceNo();
        if(StringUtils.isBlank(deviceNo)){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL.getCode(),"物联网标识不可为空");
        }
        if(deviceNoInUsed(deviceNo)){
            throw new ServiceException(BizExceptionEnum.DEVICE_NO_IN_USED.getCode(),"该设备编码已经使用");
        }


        Bridge bridge = new Bridge();
        BeanUtil.copyProperties(bridgeDto, bridge);

        this.save(bridge);
    }

    public void editBridge(BridgeDto bridgeDto) {
        Bridge bridge = new Bridge();

        BeanUtil.copyProperties(bridgeDto, bridge);
        this.updateById(bridge);
    }

    public void deleteBridge(Long bridgeId) {
        this.baseMapper.delete(bridgeId);
    }

    public Bridge getByDeviceNo(String deviceNo){
        return baseMapper.getByDeviceNo(deviceNo);
    }

    public Bridge getByBeamNo(String beamNo){
        return baseMapper.getByBeamNo(beamNo);
    }


    public boolean deviceNoInUsed(String deviceNo){
        Bridge existDevice = getByDeviceNo(deviceNo);
        return existDevice != null;
    }




}
